package com.yhb.myshop.web.admin.web.controller;

import com.yhb.myshop.domain.TbUser;
import com.yhb.myshop.web.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @RequestMapping(value = {"/login"},method = RequestMethod.GET)
    public String login(){
        return "login";

    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request, String userName, String password, Model model){
        TbUser user=userService.login(userName,password);
        if (user==null){
            model.addAttribute("message","用户名或密码错误");
            return "login";
        }
        request.getSession().setAttribute("userInfo",user);
        return "redirect:/main";

    }
}
